package com.cskaoyan.service;

import com.cskaoyan.bean.domain.*;
import com.cskaoyan.bean.wxsearch.IndexVo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.*;

/**
 * @Author lirui
 * @Date 2021/12/2 11:48
 * @describe:
 */

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public IndexVo getIndex() {
        IndexVo indexVo=new IndexVo();
        //热门关键词：hotKeyWordList
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsHotEqualTo(true);
        List<Keyword> hotKeyWords = keywordMapper.selectByExample(keywordExample);


        //默认关键词:defaultKeyWord
        KeywordExample keywordExample1=new KeywordExample();
        KeywordExample.Criteria criteria1 = keywordExample1.createCriteria();
        criteria1.andIsDefaultEqualTo(true);
        List<Keyword> keywordList1 = keywordMapper.selectByExample(keywordExample1);
        Keyword keyword = new Keyword();
        for (Keyword keyword1:keywordList1){
               keyword.setAddTime(keyword1.getAddTime());
               keyword.setDeleted(keyword1.getDeleted());
               keyword.setId(keyword1.getId());
               keyword.setIsDefault(keyword1.getIsDefault());
               keyword.setIsHot(keyword1.getIsHot());
               keyword.setKeyword(keyword1.getKeyword());
               keyword.setSortOrder(keyword1.getSortOrder());
               keyword.setUpdateTime(keyword1.getUpdateTime());
               keyword.setUrl(keyword1.getUrl());
        }
        //历史关键词：historyKeywordList
        Subject subject = SecurityUtils.getSubject();
        /**
         * 登录状态才可获取历史记录
         */
        if (subject.isAuthenticated()){
            SearchHistoryExample historyExample = new SearchHistoryExample();
            historyExample.setOrderByClause("update_time desc");
            SearchHistoryExample.Criteria historyCriteria = historyExample.createCriteria();
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            historyCriteria.andUserIdEqualTo(user.getId());
            List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(historyExample);
            indexVo.setHistoryKeywordList(searchHistories);
        }

            indexVo.setHotKeywordList(hotKeyWords);
            indexVo.setDefaultKeyword(keyword);
            return indexVo;

    }

    @Override
    public ArrayList<String> getGoodsList(String keyword) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andNameLike("%"+ keyword + "%");
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        ArrayList<String> returnData = new ArrayList<>();
        for (Goods goods1:goods){
            returnData.add(goods1.getName())   ;


        }
        return returnData;


    }


    @Override
    public void deleteHistory() {
        int i=searchHistoryMapper.deleteByExample(null);
        System.out.println(i);
    }


}
